package com.lagou.edu.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * Created by Heweipo on 2021/2/15.
 */
@RequestMapping("/email")
@RestController
@Slf4j
public class EmailController {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String emailFrom;

    @GetMapping("/{email}/{code}")
    public boolean sendSmsByEmail(@PathVariable("email") String email, @PathVariable("code") String code){

        // 发送邮件
        log.info("请求发送验证码，email:{}，code:{}",email,code);

        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("验证码");
        message.setFrom(emailFrom);
        message.setTo(email);
        message.setSentDate(new Date());
        message.setText("【learnmore】验证码：" + code + "，你正在注册 learnmore 账号，请勿向他人泄露。");
        javaMailSender.send(message);

        return true;
    }

}
